﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace Master
{
    /// <summary>
    /// Interaction logic for CtrlIpinput.xaml
    /// </summary>
    /// 
    /**
     * @Creator T.T. Dusseldorp
     * @Changes made by:
     *      - V.L. de Jager
     * */
    public partial class CtrlIpinput : UserControl
    {
        private NpOutgoing npOutgoing;
        public CtrlIpinput(NpOutgoing npOutgoing)
        {
            this.npOutgoing = npOutgoing;
            InitializeComponent();
            if (npOutgoing.Status == NpOutgoingStatus.ENABLED)
            {
                ipInputEnabled();
            }
        }

        private void ipInputEnabled()
        {
            textBox1.Visibility = System.Windows.Visibility.Collapsed;
            label1.Visibility = System.Windows.Visibility.Collapsed;
            lblTitel.Content = "networkpoint already activated";
            btnCloseConnection.Visibility = System.Windows.Visibility.Visible;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            npOutgoing.menuActive = false;
            Map.INSTANCE.Children.Remove(this);
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Client client = new Client(npOutgoing, textBox1.Text);
                npOutgoing.Status = NpOutgoingStatus.ENABLED;
                NetworkManager.INSTANCE.addClient(client);
            }
            catch (Exception exc)
            {
                npOutgoing.Status = NpOutgoingStatus.DISABLED;
                MessageBox.Show("Could not connect to this IP");
            }
            npOutgoing.menuActive = false;
            Map.INSTANCE.Children.Remove(this);
        }
         
        private void textBox1_TextInput(object sender, TextCompositionEventArgs e)
        {
            handleInput();
        }

        private void handleInput()
        {
            String regex = @"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b";
            Match match = Regex.Match(textBox1.Text, regex);
            if (match.Success)
            {
                textBox1.Background = Brushes.Green;
                btnOk.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                textBox1.Background = Brushes.DarkGray;
                btnOk.Visibility = System.Windows.Visibility.Collapsed;
            }  
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            handleInput();
        }

        private void btnCloseConnection_Click(object sender, RoutedEventArgs e)
        {
            Client client = npOutgoing.getClient();
            client.Close();
            npOutgoing.Status = NpOutgoingStatus.DISABLED;
            npOutgoing.menuActive = false;
            Map.INSTANCE.Children.Remove(this);
        }
    }
}
